package com.cisystem.system.service.impl;

import java.util.List;

import com.cisystem.common.utils.DateUtils;
import com.cisystem.system.domain.GoodCatch;
import com.cisystem.system.domain.vo.InnovationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cisystem.system.mapper.InnovationMapper;
import com.cisystem.system.domain.Innovation;
import com.cisystem.system.service.IInnovationService;

/**
 * innovationService业务层处理
 *
 * @author kelly
 * @date 2023-07-07
 */
@Service
public class InnovationServiceImpl implements IInnovationService {
    @Autowired
    private InnovationMapper innovationMapper;

    @Autowired
    private MailSendServiceImpl mailSendService;

    /**
     * 查询innovation
     *
     * @param id innovation主键
     * @return innovation
     */
    @Override
    public Innovation selectInnovationById(Long id) {
        return innovationMapper.selectInnovationById(id);
    }

    /**
     * 查询innovation列表
     *
     * @param innovation innovation
     * @return innovation
     */
    @Override
    public List<Innovation> selectInnovationList(Innovation innovation) {
        return innovationMapper.selectInnovationList(innovation);
    }

    /**
     * 新增innovation
     *
     * @param innovationVo innovationVo
     * @return 结果
     */

    @Override
    public int insertInnovation(InnovationVo innovationVo) {

        if(innovationVo.getIsSendMail()){
            //add innovation send mail
            mailSendService.sendMailByAddInnovation(innovationVo);
        }



        return innovationMapper.insertInnovation(innovationVo);
    }
    /**
     * 修改innovation
     *
     * @param innovationVo innovationVo
     * @return 结果
     */
    @Override
    public int updateInnovation(InnovationVo innovationVo) {
        if(innovationVo.getIsSendMail()){
            //add innovation send mail
            mailSendService.sendMailByUpdateInnovation(innovationVo);
        }
        return innovationMapper.updateInnovation(innovationVo);
    }

    /**
     * 批量删除innovation
     *
     * @param ids 需要删除的innovation主键
     * @return 结果
     */
    @Override
    public int deleteInnovationByIds(Long[] ids) {
        return innovationMapper.deleteInnovationByIds(ids);
    }

    /**
     * 删除innovation信息
     *
     * @param id innovation主键
     * @return 结果
     */
    @Override
    public int deleteInnovationById(Long id) {
        return innovationMapper.deleteInnovationById(id);
    }

    public  void overdueHandler(){
        //select * from ci_good_catch where status!=3 and due_date<now()
        List<Innovation> innovations = innovationMapper.selectOverdueInnovationList();
        for (Innovation innovation:innovations ){
            if( innovation.getDueDate().getTime() > DateUtils.getNowDate().getTime()){
                continue;
            }
            mailSendService.sendMailByOverdueInnovation(innovation);
        }
    }
}